<template>
  <div class='container'>
<!-- 新增技巧弹框 -->
      <el-dialog title="编辑弹窗" :visible="showAdd" @close="closeAddDialog">
        <!-- 表单内容 -->
        <template>
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
            <el-form-item label="文章标题" prop="title">
              <el-input v-model="roleForm.title" placeholder="请输入文章标签" />
            </el-form-item>
            <el-form-item label="文章内容" prop="articleBody">
              <!-- 使用 v-model 进行双向的数据绑定 -->
              <quill-editor :options="editorOption" v-model="roleForm.articleBody"></quill-editor>
            </el-form-item>
            <el-form-item label="视频地址" prop="videoURL">
              <el-input v-model="roleForm.videoURL" />
            </el-form-item>
          </el-form>
        </template>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center" class="footer">
          <el-button size="small" @click="cancel">取消</el-button>
          <el-button size="small" type="primary" @click="confirm">确定</el-button>
        </el-row>
      </el-dialog>
  </div>
</template>

<script>
// 导入富文本编辑器样式和组件
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'

import { add, update } from '@/api/hmmm/articles.js'
export default {
  watch: {
    articlesById () {
      this.roleForm.title = this.articlesById.title
      this.roleForm.articleBody = this.articlesById.articleBody
      this.roleForm.videoURL = this.articlesById.videoURL
    }
  },
  components: {
    quillEditor
  },
  props: {
    showAdd: {
      type: Boolean,
      default: false
    },
    articlesById: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      // 富文本配置项修改
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      },
      roleForm: {
        title: '',
        // 文章的内容
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 关闭弹窗
    closeAddDialog () {
      this.$emit('close_add_dialog')
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    // 确定
    confirm () {
      this.$refs.roleForm.validate(async isOk => {
        if (isOk) {
          try {
            if (this.articlesById.id) {
              await update({ ...this.roleForm, id: this.articlesById.id })
            } else {
              await add(this.roleForm)
            }
            this.$message.success(`${this.articlesById.id ? '修改' : '新增'}成功`)
            this.closeAddDialog()
            this.$emit('change_dialog')
          } catch (error) {
            this.$message.error(error)
          }
        }
      })
    },
    // 取消
    cancel () {
      this.closeAddDialog()
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .ql-editor {
    height: 200px
}
</style>
